package cn.iocoder.yudao.module.biz.service.htHiddentrouble;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentroubleannexs.HtHiddentroubleannexsDO;
import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentroubleflow.HtHiddentroubleflowDO;
import cn.iocoder.yudao.module.biz.dal.mysql.htHiddentroubleannexs.HtHiddentroubleannexsMapper;
import cn.iocoder.yudao.module.biz.dal.mysql.htHiddentroubleflow.HtHiddentroubleflowMapper;
import cn.iocoder.yudao.module.infra.dal.dataobject.oss.OssDO;
import cn.iocoder.yudao.module.infra.dal.mysql.oss.OssMapper;
import jodd.util.CollectionUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.htHiddentrouble.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htHiddentrouble.HtHiddentroubleDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.htHiddentrouble.HtHiddentroubleConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.htHiddentrouble.HtHiddentroubleMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 隐患信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HtHiddentroubleServiceImpl implements HtHiddentroubleService {

    @Resource
    private HtHiddentroubleMapper htHiddentroubleMapper;
    @Resource
    private HtHiddentroubleflowMapper htHiddentroubleflowMapper;
    @Resource
    private HtHiddentroubleannexsMapper htHiddentroubleannexsMapper;
    @Resource
    private OssMapper ossMapper;

    @Override
    public String createHtHiddentrouble(HtHiddentroubleCreateReqVO createReqVO) {
        // 隐患信息
        HtHiddentroubleDO htHiddentrouble = HtHiddentroubleConvert.INSTANCE.convert(createReqVO);
        //查询当前登录用户
        LoginUser user=SecurityFrameworkUtils.getLoginUser();
        //设置部门、用户ID
        htHiddentrouble.setUserId(user.getId());
        htHiddentrouble.setDeptId(user.getDeptId());
        //隐患流程
        HtHiddentroubleflowDO htHiddentroubleflow=new HtHiddentroubleflowDO();
        htHiddentroubleflow.setRectificationLimit(createReqVO.getRectificationLimit());
        htHiddentroubleflow.setRectificationDeptId(createReqVO.getRectificationDeptId());
        htHiddentroubleflow.setRectificationPeoId(createReqVO.getRectificationPeoId());
        htHiddentroubleflow.setRectificationType(createReqVO.getRectificationType());
        htHiddentroubleflow.setCancelDeptId(createReqVO.getCancelDeptId());
        htHiddentroubleflow.setCancelPeoId(createReqVO.getCancelPeoId());
        htHiddentroubleflow.setIssueType(createReqVO.getIssueType());
        htHiddentroubleflow.setIssuePeoId(createReqVO.getIssuePeoId());

        //检查日期为系统日期
        htHiddentrouble.setCheckDate(new Date());

        //整改方式 0：立即整改 1限期整改
        String rectificationType=createReqVO.getRectificationType();
        //下达方式 0：快速下达 1：进入隐患下达
        String issueType=createReqVO.getIssueType();
        if("0".equals(rectificationType)){
            htHiddentrouble.setState("1");
        }else if("1".equals(rectificationType)&&"0".equals(issueType)){
            //登录用户ID
            Long userId = SecurityFrameworkUtils.getLoginUserId();
            htHiddentroubleflow.setIssuePeoId(userId);
            htHiddentroubleflow.setIssueDate(new Date());
            htHiddentrouble.setState("1");
        }else if("1".equals(rectificationType)&&"1".equals(issueType)){
            htHiddentrouble.setState("0");
        }else{
            htHiddentrouble.setState("0");
        }
        htHiddentrouble.setDeleted(Boolean.FALSE);
        htHiddentroubleMapper.insert(htHiddentrouble);

        List<OssDO> ossList=createReqVO.getOssFile();
        //批量插入隐患附件表信息
        for(OssDO ossDO:ossList){
            Long ossId=ossDO.getId();
            HtHiddentroubleannexsDO entity=new HtHiddentroubleannexsDO();
            entity.setHtId(htHiddentrouble.getId());
            entity.setOssId(ossId);
            entity.setIsImg(0);
            entity.setHtStep("0");//隐患登记
            entity.setCreateTime(ossDO.getCreateTime());
            entity.setCreator(ossDO.getCreator());
            entity.setUpdateTime(ossDO.getUpdateTime());
            entity.setUpdater(ossDO.getUpdater());
            htHiddentroubleannexsMapper.insert(entity);
        }
        List<OssDO> imgList=createReqVO.getOssImg();
        //批量插入隐患图片表信息
        for(OssDO ossDO:imgList){
            Long ossId=ossDO.getId();
            HtHiddentroubleannexsDO entity=new HtHiddentroubleannexsDO();
            entity.setHtId(htHiddentrouble.getId());
            entity.setOssId(ossId);
            entity.setIsImg(1);
            entity.setHtStep("0");//隐患登记
            entity.setCreateTime(ossDO.getCreateTime());
            entity.setCreator(ossDO.getCreator());
            entity.setUpdateTime(ossDO.getUpdateTime());
            entity.setUpdater(ossDO.getUpdater());
            htHiddentroubleannexsMapper.insert(entity);
        }

        htHiddentroubleflow.setHtId(htHiddentrouble.getId());

        //插入隐患整改信息
        htHiddentroubleflowMapper.insert(htHiddentroubleflow);
        // 返回
        return htHiddentrouble.getId();
    }

    @Override
    public void updateHtHiddentrouble(HtHiddentroubleUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateHtHiddentroubleExists(updateReqVO.getId());

        // 更新隐患信息
        HtHiddentroubleDO updateObj = HtHiddentroubleConvert.INSTANCE.convert(updateReqVO);

        //隐患流程信息
        HtHiddentroubleflowDO htHiddentroubleflow=new HtHiddentroubleflowDO();
        htHiddentroubleflow.setId(updateReqVO.getFlowId());
        htHiddentroubleflow.setRectificationLimit(updateReqVO.getRectificationLimit());
        htHiddentroubleflow.setRectificationDeptId(updateReqVO.getRectificationDeptId());
        htHiddentroubleflow.setRectificationPeoId(updateReqVO.getRectificationPeoId());
        htHiddentroubleflow.setRectificationType(updateReqVO.getRectificationType());
        htHiddentroubleflow.setIssueType(updateReqVO.getIssueType());
        htHiddentroubleflow.setIssuePeoId(updateReqVO.getIssuePeoId());

        //整改方式 0：立即整改 1限期整改
        String rectificationType=updateReqVO.getRectificationType();
        //下达方式 0：快速下达 1：进入隐患下达
        String issueType=updateReqVO.getIssueType();
        if("0".equals(rectificationType)){
            updateObj.setState("3");
        }else if("1".equals(rectificationType)&&"0".equals(issueType)){
            //登录用户ID
            Long userId = SecurityFrameworkUtils.getLoginUserId();
            htHiddentroubleflow.setIssuePeoId(userId);
            htHiddentroubleflow.setIssueDate(new Date());

            updateObj.setState("3");
        }else if("1".equals(rectificationType)&&"1".equals(issueType)){
            updateObj.setState("0");
        }
        htHiddentroubleMapper.updateById(updateObj);

        List<OssDO> ossList=updateReqVO.getOssFile();
        //批量插入隐患附件表信息
        for(OssDO ossDO:ossList){
            Long ossId=ossDO.getId();
            //查询文件ID是否已存在
            int isFileExist=htHiddentroubleMapper.isFileExist(updateObj.getId(),ossId);
            if(isFileExist>0){
                continue;
            }
            HtHiddentroubleannexsDO entity=new HtHiddentroubleannexsDO();
            entity.setHtId(updateObj.getId());
            entity.setOssId(ossId);
            entity.setIsImg(0);
            entity.setHtStep("0");
            entity.setCreateTime(ossDO.getCreateTime());
            entity.setCreator(ossDO.getCreator());
            entity.setUpdateTime(ossDO.getUpdateTime());
            entity.setUpdater(ossDO.getUpdater());
            htHiddentroubleannexsMapper.insert(entity);
        }

        List<OssDO> imgList=updateReqVO.getOssImg();
        //批量插入隐患图片表信息
        for(OssDO ossDO:imgList){
            Long ossId=ossDO.getId();
            //查询文件ID是否已存在
            int isFileExist=htHiddentroubleMapper.isFileExist(updateObj.getId(),ossId);
            if(isFileExist>0){
                continue;
            }
            HtHiddentroubleannexsDO entity=new HtHiddentroubleannexsDO();
            entity.setHtId(updateObj.getId());
            entity.setOssId(ossId);
            entity.setIsImg(1);
            entity.setHtStep("0");
            entity.setCreateTime(ossDO.getCreateTime());
            entity.setCreator(ossDO.getCreator());
            entity.setUpdateTime(ossDO.getUpdateTime());
            entity.setUpdater(ossDO.getUpdater());
            htHiddentroubleannexsMapper.insert(entity);
        }

        //flowCreateReqVO.setHtId(htHiddentrouble.getId());

        //插入隐患整改信息
        htHiddentroubleflowMapper.updateById(htHiddentroubleflow);
    }

    @Override
    public void deleteHtHiddentrouble(String id) {
        // 校验存在
        this.validateHtHiddentroubleExists(id);
        // 删除
        htHiddentroubleMapper.deleteById(id);
        //TODO 删除流程信息
//        htHiddentroubleflowMapper.deleteByHtId();
    }

    private void validateHtHiddentroubleExists(String id) {
        if (htHiddentroubleMapper.selectById(id) == null) {
            throw exception(HT_HIDDENTROUBLE_NOT_EXISTS);
        }
    }

    @Override
    public HtHiddentroubleDO getHtHiddentrouble(String id) {
        return htHiddentroubleMapper.selectById(id);
    }

    @Override
    public List<HtHiddentroubleDO> getHtHiddentroubleList(Collection<String> ids) {
        return htHiddentroubleMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HtHiddentroubleDO> getHtHiddentroublePage(HtHiddentroublePageReqVO pageReqVO) {
        return htHiddentroubleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HtHiddentroubleDO> getHtHiddentroubleList(HtHiddentroubleExportReqVO exportReqVO) {
        return htHiddentroubleMapper.selectList(exportReqVO);
    }

    @Override
    public HiddentroubleJoinVO queryById(String id) {
        HiddentroubleJoinVO hiddentroubleJoinVO=htHiddentroubleMapper.queryById(id);

        int step=0;
        //TODO 根据附件、图片ID 查询
        Map params=new HashMap();
        params.put("id",id);
        params.put("step",step);
        params.put("isImg",0);
        List<String> ossFileIds=htHiddentroubleannexsMapper.getFileByHtId(params);
        if(!ossFileIds.isEmpty()) {
            List<OssDO> ossFiles = ossMapper.selectByIds(ossFileIds);
            hiddentroubleJoinVO.setOssFile(ossFiles);
        }

        params.put("isImg",1);
        List<String> ossImgIds=htHiddentroubleannexsMapper.getFileByHtId(params);
        if(!ossImgIds.isEmpty()){
            List<OssDO> ossImgs=ossMapper.selectByIds(ossImgIds);
            hiddentroubleJoinVO.setOssImg(ossImgs);
        }

        return hiddentroubleJoinVO;
    }

    @Override
    public List<HiddentroublePageJoinVO> selectHiddentroublePage(HtHiddentroublePageReqVO pageReqVO) {
        return htHiddentroubleMapper.selectHiddentroublePage(pageReqVO);
    }

    @Override
    public Integer selectHiddentroubleCount(HtHiddentroublePageReqVO pageVO) {
        return htHiddentroubleMapper.selectHiddentroubleCount(pageVO);
    }

}
